<?php
defined('BASEPATH') OR exit('No direct script access allowed');
/**
 * -----------------------------------------------------
 *
 * controllers home
 *
 * @package     controllers
 * @subpackage  
 * @category    Communal
 * @author      Pupuchen
 * @since       2015-7-9 version 1.0       
 * -----------------------------------------------------    
 */
class Communal extends CI_Controller {

	public function __construct()
	{
	  	parent::__construct();
	  	$this->load->model(array('member_model'));
	  	$this->load->library('my_md5'); 
	}
	 

	/**
	 * [forgetpass 找回密码]
	 * @return [type] [description]
	 */
	public function forgetpass(){
		$this->load->view('home/Communal/forgetpass');
	} 

	/**
	 * [getmobile 判断手机号码]
	 * @return [type] [description]
	 */
	public function getmobile(){
		$result = array(
            'err'=>'0',
            'msg'=>''
        );

		// 检查参数的合理性
		if(empty($this->input->post('mobile'))){
			$result['err'] = 1;
            $result['msg'] = '参数不正确,请重新输入!';
            exit(json_encode($result));
		}
		// 查看手机号码是否存在
		$num_mobile = $this->member_model->get_data_by_mobile($this->input->post('mobile'));
		if($num_mobile == 0){
			$result['err'] = 2;
            $result['msg'] = '该手机尚未注册!';
            exit(json_encode($result));
		}
		$result['err'] = 0;
		exit(json_encode($result));
	}

	/**
	 * [captcha 发送修改密码短信]
	 * @return [type] [description]
	 */
	public function captcha(){
		$result = array(
	        'err'=>'0',
	        'msg'=>''
        );
		// 判断数据合理性
        if(empty($this->input->post('mobile'))){
        	$result['err'] = 1;
            $result['msg'] = '手机号码错误!';
        	return json_encode($result);
        }
        // 判断手机号码是否存在
        if(!$this->member_model->get_data_by_mobile($this->input->post('mobile')) > 0){
            $result['err'] = 2;
            $result['msg'] = '此手机不存在';
            exit(json_encode($result));
        }
		$mobile 	= $this->input->post('mobile'); // 手机号码
		$this->load->library('mobile_sns',array('mobile'=>$mobile));   	
		$captcha = $this->mobile_sns->create_char();	
		// 把验证码加入session中			   
		$_SESSION['pass_captcha'] = $captcha; // 
		$reg = $this->mobile_sns->yzm('本次操作的验证码为:'.$captcha.',30分钟内有效，如非您本人操作,请忽略该短信。');
		if($reg == 'ok'){
			$result['err'] = 0;
            exit(json_encode($result));
		}else{
			$result['err'] = 3;
            $result['msg'] = $this->mobile_sns->error;
            exit(json_encode($result));
		}
	}

	/**
	 * [yzcaptcha 判断验证码是否正确]
	 * @return [type] [description]
	 */
	public function yzcaptcha(){
		$result = array(
	        'err'=>'0',
	        'msg'=>''
        );
		// 判断数据合理性
        if(empty($this->input->post('captcha'))){
        	$result['err'] = 1;
            $result['msg'] = '参数错误,请重新填写!';
        	return json_encode($result);
        }

        if($_SESSION['pass_captcha'] != $this->input->post('captcha')){
        	$result['err'] = 2;
            $result['msg'] = '验证码填写错误,请重新填写!';
        	return json_encode($result);
        }
        $result['err'] = 0;
        exit(json_encode($result));
	}

	/**
	 * [member_update 更改用户数据]
	 * @return [type] [description]
	 */
	public function member_update(){
		$result = array(
	        'err'=>'0',
	        'msg'=>''
        );
		// 判断数据合理性
        if(empty($this->input->post('password'))){
        	$result['err'] = 1;
            $result['msg'] = '参数错误,请重新填写!';
        	return json_encode($result);
        }

        $arr_member = $this->member_model->get_a_member_by_mid($_SESSION['member_id']); // 会员信息 
		
		$data['password'] = $this->my_md5->to_md5($this->input->post('password'));

		if($this->member_model->update($data,$_SESSION['member_id'])){
			$result['err'] = 0;
            exit(json_encode($result));
		}else{
			$result['err'] = 2;
            $result['msg'] = '更新失败,请重新找回密码!';
            exit(json_encode($result));
		}
	}

	/**
	 * [login 重新登录]
	 * @return [type] [description]
	 */
	public function login(){
		if(!empty($_SESSION['member_id'])){
			unset($_SESSION['member_id']);
			unset($_SESSION['member_mobile']);
			header('Location:'.base_url('signup/login'));
            exit;
		}
	}

}
?>